// 使用统一的请求实例，自动携带token
import instance from '@/utils/request'

// 为了兼容现有代码，创建一个api对象（代理到instance）
// 注意：响应拦截已由 @/utils/request 统一处理
const api = {
  get: (url, config = {}) => {
    // 处理params参数
    if (config.params) {
      return instance.request({ method: 'GET', url, params: config.params, ...config })
    }
    return instance.request({ method: 'GET', url, ...config })
  },
  post: (url, data, config = {}) => instance.request({ method: 'POST', url, data, ...config }),
  put: (url, data, config = {}) => instance.request({ method: 'PUT', url, data, ...config }),
  delete: (url, config = {}) => instance.request({ method: 'DELETE', url, ...config }),
  patch: (url, data, config = {}) => instance.request({ method: 'PATCH', url, data, ...config })
}

// 企业相关API
export const enterpriseAPI = {
  // 获取企业列表
  getEnterpriseList(params = {}) {
    return api.get('/enterprises', { params })
  },

  // 创建企业
  createEnterprise(data) {
    return api.post('/enterprises', data)
  },

  // 获取企业信息
  getEnterpriseInfo(enterpriseId) {
    return api.get(`/enterprises/${enterpriseId}`)
  },

  // 更新企业信息
  updateEnterpriseInfo(enterpriseId, data) {
    return api.put(`/enterprises/${enterpriseId}`, data)
  },

  // 删除企业
  deleteEnterprise(enterpriseId) {
    return api.delete(`/enterprises/${enterpriseId}`)
  },

  // 获取企业统计数据
  getEnterpriseStatistics(enterpriseId) {
    return api.get(`/enterprises/${enterpriseId}/statistics`)
  },

  // 获取企业评价列表
  getEnterpriseReviews(enterpriseId, params = {}) {
    return api.get(`/enterprises/${enterpriseId}/reviews`, { params })
  },

  // 创建企业评价
  createReview(data) {
    return api.post('/reviews', data)
  },

  // 回复企业评价
  replyEnterpriseReview(reviewId, replyContent) {
    return api.post(`/reviews/${reviewId}/reply`, { replyContent })
  },

  // 处理企业投诉
  processEnterpriseComplaint(complaintId, processData) {
    return api.post(`/complaints/${complaintId}/process`, processData)
  }
}

// 岗位相关API
export const jobAPI = {
  // 获取岗位列表（多维度筛选 + 关键词搜索）
  getJobList(params = {}) {
    return api.get('/jobs', { params })
  },

  // 获取岗位详情
  getJobDetail(jobId) {
    return api.get(`/jobs/${jobId}`)
  },

  // 创建岗位
  createJob(data) {
    return api.post('/jobs', data)
  },

  // 更新岗位
  updateJob(jobId, data) {
    return api.put(`/jobs/${jobId}`, data)
  },

  // 删除岗位
  deleteJob(jobId) {
    return api.delete(`/jobs/${jobId}`)
  },

  // 更新岗位状态（上线/下线）
  updateJobStatus(jobId, status) {
    return api.put(`/jobs/${jobId}/status`, { status })
  },

  // 更新岗位置顶状态
  updateJobTopStatus(jobId, isTop) {
    return api.put(`/jobs/${jobId}/top`, { isTop })
  },

  // 获取岗位投递数据
  getJobApplicationData(jobId) {
    return api.get(`/jobs/${jobId}/applications`)
  },

  // 投递岗位
  applyJob(jobId, applicationData) {
    return api.post(`/jobs/${jobId}/apply`, applicationData)
  },

  // 收藏岗位
  collectJob(jobId) {
    return api.post(`/jobs/${jobId}/collect`)
  },

  // 取消收藏岗位
  uncollectJob(jobId) {
    return api.delete(`/jobs/${jobId}/collect`)
  },

  // 检查岗位是否已收藏
  checkJobCollected(jobId) {
    return api.get(`/jobs/${jobId}/check-collected`)
  },

  // 获取用户收藏的岗位列表
  getUserFavoriteJobs(params = {}) {
    return api.get('/user/favorite-jobs', { params })
  },

  // 获取用户投递记录
  getUserApplications(params = {}) {
    return api.get('/user/applications', { params })
  },

  // 获取企业投递记录
  getEnterpriseApplications(enterpriseId, params = {}) {
    return api.get(`/enterprises/${enterpriseId}/applications`, { params })
  },

  // 更新投递状态
  updateApplicationStatus(applicationId, status) {
    return api.put(`/applications/${applicationId}/status`, { status })
  },

  // 撤回投递
  withdrawApplication(applicationId) {
    return api.delete(`/applications/${applicationId}`)
  },

  // 批量操作岗位
  batchOperateJobs(jobIds, operation) {
    return api.post('/jobs/batch', { jobIds, operation })
  }
}

// 评价相关API
export const reviewAPI = {
  // 获取企业评价列表
  getEnterpriseReviews(enterpriseId, params = {}) {
    return api.get(`/enterprises/${enterpriseId}/reviews`, { params })
  },

  // 删除评价
  deleteReview(reviewId) {
    return api.delete(`/reviews/${reviewId}`)
  },

  // 回复评价
  replyReview(reviewId, replyContent) {
    return api.put(`/reviews/${reviewId}/reply`, { replyContent })
  },

  // 举报评价
  reportReview(reviewId, reportReason) {
    return api.post(`/reviews/${reviewId}/report`, { reportReason })
  },

  // 处理投诉
  processComplaint(complaintId, processData) {
    return api.post(`/complaints/${complaintId}/process`, processData)
  },

  // 获取投诉列表
  getComplaints(params = {}) {
    return api.get('/complaints', { params })
  }
}

// 员工相关API
export const employeeAPI = {
  // 获取企业员工列表
  getEnterpriseEmployees(enterpriseId, params = {}) {
    return api.get(`/enterprises/${enterpriseId}/employees`, { params })
  },

  // 创建员工
  createEmployee(data) {
    return api.post('/employees', data)
  },

  // 更新员工信息
  updateEmployee(employeeId, data) {
    return api.put(`/employees/${employeeId}`, data)
  },

  // 删除员工
  deleteEmployee(employeeId) {
    return api.delete(`/employees/${employeeId}`)
  },

  // 批量操作员工
  batchOperateEmployees(employeeIds, operation) {
    return api.post('/employees/batch', { employeeIds, operation })
  }
}

// 单独导出常用API函数
export const getJobList = jobAPI.getJobList
export const getJobDetail = jobAPI.getJobDetail
export const createJob = jobAPI.createJob
export const updateJob = jobAPI.updateJob
export const deleteJob = jobAPI.deleteJob
export const updateJobStatus = jobAPI.updateJobStatus
export const updateJobTopStatus = jobAPI.updateJobTopStatus
export const getJobApplicationData = jobAPI.getJobApplicationData
export const applyJob = jobAPI.applyJob
export const collectJob = jobAPI.collectJob
export const uncollectJob = jobAPI.uncollectJob
export const checkJobCollected = jobAPI.checkJobCollected
export const getUserFavoriteJobs = jobAPI.getUserFavoriteJobs
export const getUserApplications = jobAPI.getUserApplications
export const getEnterpriseApplications = jobAPI.getEnterpriseApplications
export const updateApplicationStatus = jobAPI.updateApplicationStatus
export const withdrawApplication = jobAPI.withdrawApplication
export const batchOperateJobs = jobAPI.batchOperateJobs

export const getEnterpriseList = enterpriseAPI.getEnterpriseList
export const createEnterprise = enterpriseAPI.createEnterprise
export const getEnterpriseInfo = enterpriseAPI.getEnterpriseInfo
export const updateEnterpriseInfo = enterpriseAPI.updateEnterpriseInfo
export const deleteEnterprise = enterpriseAPI.deleteEnterprise
export const uploadEnterpriseImage = enterpriseAPI.uploadEnterpriseImage
export const getEnterpriseStatistics = enterpriseAPI.getEnterpriseStatistics
export const getEnterpriseReviews = enterpriseAPI.getEnterpriseReviews
export const createReview = enterpriseAPI.createReview
export const replyEnterpriseReview = enterpriseAPI.replyEnterpriseReview
export const processEnterpriseComplaint = enterpriseAPI.processEnterpriseComplaint

export const getReviews = reviewAPI.getEnterpriseReviews
export const deleteReview = reviewAPI.deleteReview
export const replyReview = reviewAPI.replyReview
export const reportReview = reviewAPI.reportReview
export const processComplaint = reviewAPI.processComplaint
export const getComplaints = reviewAPI.getComplaints

export const getEmployees = employeeAPI.getEnterpriseEmployees
export const createEmployee = employeeAPI.createEmployee
export const updateEmployee = employeeAPI.updateEmployee
export const deleteEmployee = employeeAPI.deleteEmployee
export const batchOperateEmployees = employeeAPI.batchOperateEmployees

// 通用API工具函数
export const apiUtils = {
  // 处理分页参数
  formatPaginationParams(page, pageSize) {
    return {
      page: page || 1,
      pageSize: pageSize || 10
    }
  },

  // 处理搜索参数
  formatSearchParams(keyword, filters = {}) {
    const params = {}
    if (keyword) params.keyword = keyword
    Object.assign(params, filters)
    return params
  },

  // 处理API错误
  handleApiError(error) {
    const message = error.response?.data?.message || error.message || '请求失败'
    console.error('API错误:', message)
    return message
  }
}

export default api